package service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import models.Fornecedor;
import models.Usuario;
import play.db.DB;
import exceptions.BusinessException;

public class UsuarioDAO {

	private Connection connection = null;
	private Statement statement = null;
	private ResultSet resultSet = null;
	
	public boolean validarUsuario(Usuario user) throws SQLException, BusinessException{
		
		connection = DB.getConnection();
		
			statement = connection.createStatement();
			
			String query = "SELECT * from USUARIO WHERE DSUSUARIO = '"+ user.usuario + "';";
			
			resultSet = statement
			          .executeQuery(query);
			Usuario bdUser = writeResultSet(resultSet);
			
			if(bdUser==null){
				return false;
			}else if(user.senha.equals(bdUser.senha)){
				return true;
			}
			throw new BusinessException();
		
	}
	
	public void cadastrarUsuario(Usuario usuario) throws SQLException,
			BusinessException {

		connection = DB.getConnection();

		statement = connection.createStatement();

		StringBuffer query = new StringBuffer();

		query.append("SELECT * from USUARIO WHERE DSUSUARIO = '"
				+ usuario.usuario + "';");

		resultSet = statement.executeQuery(query.toString());
		Usuario bdFornecedor = writeResultSet(resultSet);

		if (bdFornecedor == null) {
			query = new StringBuffer();
			query.append("INSERT INTO USUARIO(DSUSUARIO,PASSWORD) VALUES (");
			query.append("'" + usuario.usuario + "',");
			query.append("'" + usuario.senha + "'");
			query.append(");");
			System.out.println(query.toString());

			statement.execute(query.toString());
		} else {
			throw new BusinessException();
		}
	}
	
	
	public List<Usuario> buscarTodosUsuarios() throws SQLException, BusinessException {
		connection = DB.getConnection();

		statement = connection.createStatement();

		StringBuffer query = new StringBuffer();

		query.append("SELECT * from USUARIO;");

		resultSet = statement.executeQuery(query.toString());
		List<Usuario> bdFornecedor = writeResultSetList(resultSet);

		return bdFornecedor;
		
	}
	
	private Usuario writeResultSet(ResultSet resultSet) throws SQLException {
	    // resultSet is initialised before the first data set
		Usuario user = null;
	    while (resultSet.next()) {
	      user = new Usuario();
	      int id = resultSet.getInt("IDUSUARIO");
	      String nome = resultSet.getString("DSUSUARIO");
	      String password = resultSet.getString("PASSWORD");
	      
	      user.id = id;
	      user.usuario = nome;
	      user.senha = password;
	      
	    }
	    
	    return user;
	  }

	private List<Usuario> writeResultSetList(ResultSet resultSet)
			throws SQLException {
		// resultSet is initialised before the first data set
		Usuario usuario = null;

		List<Usuario> result = new ArrayList<Usuario>();

		while (resultSet.next()) {
			usuario = new Usuario();
			int id = resultSet.getInt("IDUSUARIO");
			String nome = resultSet.getString("DSUSUARIO");
			String password = resultSet.getString("PASSWORD");

			usuario.id = id;
			usuario.usuario = nome;
			usuario.senha = password;
			result.add(usuario);
		}
		if (result.size() == 0) {
			return null;
		}
		return result;
	}

}
